﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Organizer
{
    /// <summary>
    /// Логика взаимодействия для Create.xaml
    /// </summary>
    public partial class Create : Window
    {
        private Task temp;
        private bool flag=false;

        public Create()
        {
            InitializeComponent();
        }

        public Create(Task tmp)
        {
            InitializeComponent();

            t_name.Focus();

            // flag = true - EDIT
            // flag = false - CREATE

            temp = tmp;                          
            if (tmp.name == "")               // New
                temp.id = DataBase.getMaxId();
            else                              // Edit
            {
                flag = true;

                t_name.Text = temp.name;
                t_def.Text = temp.definition;
                t_sdate.SelectedDate = temp.startDate;
                t_edate.SelectedDate = temp.endDate;
                t_hour.Text = temp.hours.ToString();
                t_min.Text = temp.mins.ToString();
            }
                
        }


        private void Create_Click(object sender, RoutedEventArgs e)
        {
            if (t_name.Text == "")
            {
                MessageBox.Show("Введите имя задачи!", "Error");
                t_name.Focus();
            }
            else
            {
                int Thour = 0;
                int Tmin = 0;
                int.TryParse(t_hour.Text, out Thour);
                int.TryParse(t_min.Text, out Tmin);
                DateTime Sdate = DateTime.Now;
                DateTime Edate = DateTime.Now;
                if (t_sdate.SelectedDate != null)
                    DateTime.TryParse(t_sdate.SelectedDate.Value.ToString(), out Sdate);
                if (t_edate.SelectedDate != null)
                    DateTime.TryParse(t_edate.SelectedDate.Value.ToString(), out Edate);

                temp.name = t_name.Text;
                temp.definition = t_def.Text;
                temp.startDate = Sdate;
                temp.endDate = Edate;
                temp.hours = Thour;
                temp.mins = Tmin;

                if (flag)
                    DataBase.Update(temp);
                else
                {
                    DataBase.InsDB(temp);
                    DataBase.isName = true;
                }

                this.Close();
            }

        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void t_hour_TextChanged(object sender, TextChangedEventArgs e)
        {
        if (t_hour.Text.Length!=0)
            if (!char.IsDigit(t_hour.Text[t_hour.Text.Length-1]))
            {
                if (t_hour.Text.Length > 1)
                    t_hour.Text = t_hour.Text.Substring(0, t_hour.Text.Length - 1);
                else t_hour.Text = "";
            }
        }


        private void t_min_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (t_min.Text.Length != 0)
                if (!char.IsDigit(t_min.Text[t_min.Text.Length - 1]))
                {
                    if (t_min.Text.Length > 1)
                        t_min.Text = t_min.Text.Substring(0, t_min.Text.Length - 1);
                    else t_min.Text = "";
                }
                else
                {
                    int t = Convert.ToInt32(t_min.Text.ToString());
                    if (t >= 60)
                    {
                        t_min.Text = "59";
                    }

                }
        }

 

    }
}
